package cn.iocoder.yudao.module.crm.service.tasktype;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.crm.controller.admin.tasktype.vo.TaskTypeDetailReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.tasktype.vo.TaskTypePageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.tasktype.vo.TaskTypeSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.tasktype.TaskTypeDO;
import cn.iocoder.yudao.module.crm.dal.mysql.tasktype.TaskTypeApproverMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.tasktype.TaskTypeAssigneeMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.tasktype.TaskTypeContentMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.tasktype.TaskTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.TASK_TYPE_NOT_EXISTS;

/**
 * CRM 任务单类型 Service 实现类
 *
 * @author 广州氪量
 */
@Service
@Validated
public class TaskTypeServiceImpl implements TaskTypeService {

    @Resource
    private TaskTypeMapper taskTypeMapper;
    @Resource
    private TaskTypeAssigneeMapper taskTypeAssigneeMapper;
    @Resource
    private TaskTypeApproverMapper taskTypeApproverMapper;
    @Resource
    private TaskTypeContentMapper taskTypeContentMapper;

    @Override
    @Transactional
    public Long createTaskType(TaskTypeDetailReqVO createReqVO) {
        // 插入
        TaskTypeDO taskType = BeanUtils.toBean(createReqVO, TaskTypeDO.class);
        taskTypeMapper.insert(taskType);
        taskTypeAssigneeMapper.insertBatchByTaskTypeId(createReqVO.getAutoIssue(), taskType.getId());
        taskTypeApproverMapper.insertBatchByTaskTypeId(createReqVO.getApprover(), taskType.getId());
        taskTypeContentMapper.insertBatchByTaskTypeId(createReqVO.getContent(), taskType.getId());
        // 返回
        return taskType.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long updateTaskType(TaskTypeDetailReqVO updateReqVO) {
        // 校验存在
        validateTaskTypeExists(updateReqVO.getId());
        // 更新
        TaskTypeDO updateObj = BeanUtils.toBean(updateReqVO, TaskTypeDO.class);
        taskTypeMapper.updateById(updateObj);
        // 先删除，后插入
        taskTypeAssigneeMapper.deleteBatchByTaskTypeId(updateReqVO.getId());
        taskTypeAssigneeMapper.insertBatchByTaskTypeId(updateReqVO.getAutoIssue(), updateReqVO.getId());
        taskTypeApproverMapper.deleteBatchByTaskTypeId(updateReqVO.getId());
        taskTypeApproverMapper.insertBatchByTaskTypeId(updateReqVO.getApprover(), updateReqVO.getId());
        taskTypeContentMapper.deleteBatchByTaskTypeId(updateReqVO.getId());
        taskTypeContentMapper.insertBatchByTaskTypeId(updateReqVO.getContent(), updateReqVO.getId());
        return updateObj.getId();
    }

    @Override
    public void deleteTaskType(Long id) {
        // 校验存在
        validateTaskTypeExists(id);
        // 删除
        taskTypeMapper.deleteById(id);
    }

    private void validateTaskTypeExists(Long id) {
        if (taskTypeMapper.selectById(id) == null) {
            throw exception(TASK_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public TaskTypeDO getTaskType(Long id) {
        return taskTypeMapper.selectById(id);
    }

    @Override
    public PageResult<TaskTypeDO> getTaskTypePage(TaskTypePageReqVO pageReqVO) {
        return taskTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TaskTypeDO> getTaskTypeList() {
        return taskTypeMapper.selectList();
    }

    @Override
    public Map<Long, String> getTaskTypeNamesByIds(Set<Long> taskTypeIds) {
        return taskTypeMapper.selectNameMap(taskTypeIds);
    }
}